package com.bjsxt.controller;

import com.bjsxt.pojo.Admin;
import com.bjsxt.pojo.PageA;
import com.bjsxt.pojo.Role;
import com.bjsxt.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    /*用户注销*/
    @RequestMapping("/adminLoginOut")
    public String adminLoginOut(HttpSession session){
        //手动注销session
        session.invalidate();
        //重定向跳转
        return  "redirect:/login.jsp";
    }

    /*用户登录*/
    @ResponseBody
    @RequestMapping("/adminLogin")
    public boolean adminLogin(String aname, String apwd, HttpSession session){
        Admin admin = adminService.login(aname, apwd);
        if (admin!=null){
            session.setAttribute("admin",admin);
            return true;
        }
        return false;
    }


    //查询全部的用户信息
    @RequestMapping("/fingMoreAdmin")
    @ResponseBody
    public PageA<Admin> fingMoreAdmin(Integer page,Integer rows){
        return  adminService.findAllAdmin(page, rows);
    }
    //添加用户信息
    @RequestMapping("/saveAdmin")
    @ResponseBody
    public Integer saveAdmin(Admin admin,Integer rid){
        return adminService.save(admin,rid);
    }

    //删除用户.
    @RequestMapping("/deleteAdmin")
    @ResponseBody
    public Integer deleteAdmin(Integer aid){
        return  adminService.detele(aid);
    }


    @RequestMapping("/findOneRole")
    @ResponseBody
    //查询指定用户对用的角色
    public Integer findOneRole(Integer aid){
        return adminService.findOneRole(aid);
    }

    //修改指定用户的信息
    @RequestMapping("/updateAdmin")
    @ResponseBody
    public Integer updateAdmin(Admin admin,Integer rid){
        return  adminService.update(admin, rid);
    }
}
